//
//  main.m
//  IdAndCTypeTest
//
//  Created by wangrui on 14-4-25.
//  Copyright (c) 2014年 PPS. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface TestObject : NSObject

@property BOOL isSomething;
@property double number;
@property long longnum;


- (void)runTest;

@end


@implementation TestObject

- (int)testReturnInt {
    return 100;
}

- (double)testReturnDouble {
    return 1562.25583;
}

- (NSNumber *)testReturenNSNumber {
    return [NSNumber numberWithInt:50];
}


- (void)runTest {
    
    //官方文档解释
    //for methods that return anything other than an object, use NSInvocation.
    
    int val0 = [self performSelector:@selector(testReturnInt)]; //警告,id转int,long,long long
    NSLog(@"%d",val0);//输出是正确的
    
    
    id val1 = [self performSelector:@selector(testReturnInt)]; //arc下这样写会一定挂掉 why? (换成testReturnDouble不会挂，有时候也会挂)
    //NSLog(@"%@",val1);//崩溃,val1不是对象
    NSLog(@"%d",val1);//输出也是正确

    

//    double val2 = [self performSelector:@selector(testReturnDouble)]; //报错,id不能转double
//    NSLog(@"%d",val2);
    
    id val3 = [self performSelector:@selector(testReturnDouble)];
    //    NSLog(@"%@",val3); //崩溃,val3不是对象
    NSLog(@"%ld,%f",val3,val3);
    
    
//    NSNumber *val4 = [self performSelector:@selector(testReturnDouble)];
//    NSLog(@"%@",val4); //挂掉
    
    
    
    /*
     *测试 NSNumber
     */
    id val = [self testReturenNSNumber];
    NSLog(@"%lld,%@,%p",val,val,val);//12839,50,0x3227  %lld把地址变为10进制打出来了
    
    
    NSLog(@"\n\n==============================Seperator==================================>>>\n\n");
    
    /*
     * setValue 设置基本类型的值
     *
     * 使用performSelector 传 基本类型给函数事，直接用NSNumber, 会直接转换过去
     *
     */
    //  设置bool 可行
    //    NSNumber *n = @(NO);
    //    if ([self validateValue:&n forKey:@"isSomething" error:nil]) {
    //        [self setValue:n forKey:@"isSomething"];
    //    }
    //
    //    NSLog(@"%d",self.isSomething);
    
    
    // 设置double 可行
    //    NSNumber *d = @(156.3256);
    //    if ([self validateValue:&d forKey:@"number" error:nil]) {
    //        [self setValue:d forKey:@"number"];
    //    }
    //
    //    NSLog(@"%f",self.number);
    
    //    NSNumber *d = @(156.3256);
    //    if ([self validateValue:&d forKey:@"number" error:nil]) {
    //        [self setValue:nil forKey:@"number"];
    //    }
    
    
    //    long *l = 156;
    //    if ([self validateValue:&l forKey:@"number" error:nil]) {
    //        [self setValue:l forKey:@"number"];//崩溃,需要传入对象类型
    //    }
    //    NSLog(@"%ld",l);

    //    NSLog(@"%d",self.isSomething);
    //    NSLog(@"%f",self.number);

}

@end


int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        [[TestObject new] runTest];
        
    }
    return 0;
}

